<?php

namespace App\Model;

use App\Traits\ErrorTrait;
use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    use ErrorTrait;
    protected $guarded = [];
    public $timestamps = false;
    protected $table = 'address';

    /**
     * 获取地址列表
     * @param int $uid
     * @return mixed
     */
    public function getAddressList(int $uid)
    {
        return $this->where('uid', $uid)->select('id', 'area', 'address', 'status', 'phone', 'receiver')->get();
    }

    /**
     * 验证参数
     * @param $param
     * @param $mustParam
     */
    public function verifyParam($param, $mustParam)
    {
        $param = array_keys($param);
        $re = array_diff($mustParam, $param);
        if ($re) {
            $this->error = '参数缺失';
            return false;
        }
        return true;
    }

    /**
     * 添加地址
     * @param $param
     * @return bool
     */
    public function addAddress($param)
    {
        $mustParam = ['uid', 'province', 'area', 'city', 'address', 'status', 'phone', 'receiver', 'province_code', 'area_code', 'city_code'];
        $re = $this->verifyParam($param, $mustParam);
        if (!$re) return false;
        $insert = [
            'uid' => $param['uid'],
            'area' => $param['province'] . '-' . $param['city'] . '-' . $param['area'],
            'address' => $param['address'],
            'status' => $param['status'],
            'phone' => $param['phone'],
            'receiver' => $param['receiver'],
            'province_code' => $param['province_code'],
            'area_code' => $param['area_code'],
            'city_code' => $param['city_code'],
            'create_time' => time()
        ];
        if ($param['status'] == 1) {
            $this->where('status', 1)->update(['status' => 0]);
        }
        $re = $this->insert($insert);
        if (!$re) {
            $this->error = '参数异常';
            return false;
        }
        return true;

    }

    /**
     * 编辑收货地址
     * @param $param
     * @return bool
     */
    public function editAddress($param)
    {
        if (!$param['address_id']) {
            $this->error = '参数缺失';
            return false;
        }
        if (isset($param['status']) && $param['status'] == 1) {
            $this->where('status', 1)->where('id', '<>', $param['address_id'])->update(['status' => 0]);
        }
        $update = [];
        if (isset($param['province'])) {
            $update['area'] = $param['province'] . '-' . $param['city'] . '-' . $param['area'];
            $update['province_code'] = $param['province_code'];
            $update['area_code'] = $param['area_code'];
            $update['city_code'] = $param['city_code'];
        }
        if (isset($param['status'])) {
            $update['status'] = $param['status'];
        }
        if (isset($param['address'])) {
            $update['address'] = $param['address'];
        }
        if (isset($param['phone'])) {
            $update['phone'] = $param['phone'];
        }
        if (isset($param['receiver'])) {
            $update['receiver'] = $param['receiver'];
        }
        $this->where('id', $param['address_id'])->where('uid', $param['uid'])->update($update);
        return true;
    }

    /**
     * 获取收货地址
     * @param $addressId
     * @return mixed
     */
    public function getAddress($addressId)
    {
        return $this->where('id', $addressId)->select('id', 'area', 'address', 'status', 'phone', 'receiver', 'province_code', 'area_code', 'city_code')->first();
    }

    /**
     * 删除收货地址
     * @param $addressId
     * @param $uid
     * @return bool
     */
    public function delAddress($addressId, $uid)
    {
        $this->where('uid', $uid)->where('id', $addressId)->delete();
        return true;
    }

    /**
     * 获取默认收货地址
     * @param $uid
     * @return mixed
     */
    public function getDefaultAddress($uid)
    {
        $address = $this->where('uid', $uid)->where('status', 1)
            ->select('id', 'area', 'address', 'status', 'phone', 'receiver')->first();

        if (!$address) {
            $address = $this->where('uid', $uid)->orderBy('id', 'desc')
                ->select('id', 'area', 'address', 'status', 'phone', 'receiver')->first();
        }
        return $address;
    }
}
